package com.cabrida.livestar.main.me;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.main.MainActivity;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.view.CircleImageView;

/**
 * 修改个人资料
 */
public class InformationActivity extends BaseActivity {
	public static final String TAG = InformationActivity.class.getSimpleName();
	private ImageView iv_back;
	private TextView tv_title;
	private CircleImageView iv_header;
	private LinearLayout ll_header;
	private LinearLayout ll_name;
	private TextView tv_name;
	private LinearLayout ll_sex;
	private TextView tv_sex;
	private LinearLayout ll_age;
	private TextView tv_age;
	private LinearLayout ll_city;
	private TextView tv_city;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
	}

	private BroadcastReceiver mReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(MainActivity.ACTION_EDIT_INFORMATION)) {
				refreshPersonalData();
			}
		}
	};

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_information);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		ll_header = (LinearLayout) findViewById(R.id.ll_header);
		iv_header = (CircleImageView) findViewById(R.id.iv_header);
		ll_name = (LinearLayout) findViewById(R.id.ll_name);
		tv_name = (TextView) findViewById(R.id.tv_name);
		ll_sex = (LinearLayout) findViewById(R.id.ll_sex);
		tv_sex = (TextView) findViewById(R.id.tv_sex);
		ll_age = (LinearLayout) findViewById(R.id.ll_age);
		tv_age = (TextView) findViewById(R.id.tv_age);
		ll_city = (LinearLayout) findViewById(R.id.ll_city);
		tv_city = (TextView) findViewById(R.id.tv_city);
	}

	/**
	 * 更新数据
	 */
	private void refreshPersonalData() {
		MyUtils.loadImg(Global.headPic, iv_header);
		tv_name.setText(Global.U_nickname);
		tv_age.setText(Global.U_age);
		tv_city.setText(Global.U_city);
		if (!TextUtils.isEmpty(Global.U_sex)) {// sex 0 没填 1男 2女
			if ("2".equals(Global.U_sex)) {
				tv_sex.setText("女");
			} else {
				tv_sex.setText("男");
			}
		}
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		ll_name.setOnClickListener(this);
		ll_sex.setOnClickListener(this);
		ll_age.setOnClickListener(this);
		ll_city.setOnClickListener(this);
		tv_title.setText(getResources().getString(
				R.string.fans_change_information));
		ll_header.setOnClickListener(this);

		refreshPersonalData();

		IntentFilter filter_ad = new IntentFilter();
		filter_ad.addAction(MainActivity.ACTION_EDIT_INFORMATION);
		registerReceiver(mReceiver, filter_ad);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.ll_header:
			openActivity(PersonalChangeHeaderActivity.class);
			break;
		case R.id.ll_name:
			openActivity(PersonalChangeNameActivity.class);
			break;
		case R.id.ll_sex:
			openActivity(PersonalChangeSexActivity.class);
			break;
		case R.id.ll_age:
			openActivity(PersonalChangeAgeActivity.class);
			break;
		case R.id.ll_city:
			openActivity(PersonalChangeCityActivity.class);
			break;

		default:
			break;
		}
	}
}
